package com.chixing.day14_map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Foo {
    private String code;
    private String name;

    public Foo(String code, String name) {
        this.code = code;
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Foo{" +
                "code='" + code + '\'' +
                ", name='" + name + '\'' +
                '}';
    }

    public static void main(String[] args) {
        Foo f1 = new Foo("测试1", "test a");
        Foo f2 = new Foo("测试2", "test b");
        Foo f3 = new Foo("测试1", "test c");
        Foo f4 = new Foo("测试3", "test d");
        Foo f5 = new Foo("测试3", "test e");
        Foo f6 = new Foo("测试4", "test f");
        List<Foo> list = new ArrayList<>();
        list.add(f1);
        list.add(f2);
        list.add(f3);
        list.add(f4);
        list.add(f5);
        list.add(f6);
        //System.out.println(list);

        Map<String,List<Foo>> foomap = new HashMap<>();
        for (Foo foo : list){
            String code = foo.getCode();
            if (foomap.containsKey(code)){
                List<Foo> fooList = foomap.get(code);
                fooList.add(foo);
                //foomap.put(code,fooList);
            }else {
                List<Foo> fooList = new ArrayList<>();
                fooList.add(foo);
                foomap.put(code, fooList);
            }
        }
        System.out.println(foomap);





        /*Map<String,List<Foo>> map1 = new HashMap<>();
        for (int i=0;i<list.size();i++){
            List<Foo> list1 = new ArrayList<>();
            for (int j=i;j<list.size();j++){
                if (list.get(i).getCode().equals(list.get(j).getCode()))
                    list1.add(list.get(i));
            }
            if (!map1.containsKey(list.get(i).getCode())){
                map1.put(list.get(i).getCode(),list1);
            }
        }
        System.out.println(map1);

        for (String i :map1.keySet()){
            System.out.println("测试:"+i+",List:"+map1.get(i));
        }*/

    }


}